﻿#include "appconfig.h"
#include "quihelper.h"

QString AppConfig::ConfigFile = "config.ini";

int AppConfig::IndexDemo = 0;
int AppConfig::LeftWidth = 150;
int AppConfig::RightWidth = 200;

QString AppConfig::LocalDbType = "Sqlite";
QString AppConfig::LocalDbName = "dbtool";
QString AppConfig::LocalHostName = "127.0.0.1";
int AppConfig::LocalHostPort = 3306;
QString AppConfig::LocalUserName = "root";
QString AppConfig::LocalUserPwd = "root";

QString AppConfig::DbType2 = "Sqlite";
QString AppConfig::DbName2 = "dbtool";
QString AppConfig::HostName2 = "127.0.0.1";
int AppConfig::HostPort2 = 3306;
QString AppConfig::UserName2 = "root";
QString AppConfig::UserPwd2 = "root";

QString AppConfig::SqlColumn2 = "insert into LogInfo(LogID,TriggerTime,UserName,LogCh,LogType,LogContent)";
QString AppConfig::SqlValue2 = QString::fromUtf8("values('AUTO_INCREMENT','DATETIME','admin','0','用户操作','测试数据')");
int AppConfig::Count2 = 1000;
int AppConfig::Interval2 = 1000;

QString AppConfig::DbType3 = "Sqlite";
QString AppConfig::DbName3 = "dbtool";
QString AppConfig::HostName3 = "127.0.0.1";
int AppConfig::HostPort3 = 3306;
QString AppConfig::UserName3 = "root";
QString AppConfig::UserPwd3 = "root";

int AppConfig::Count3 = 1;
int AppConfig::Interval3 = 1;

QString AppConfig::DbType4 = "Sqlite";
QString AppConfig::DbName4 = "dbtool";
QString AppConfig::HostName4 = "127.0.0.1";
int AppConfig::HostPort4 = 3306;
QString AppConfig::UserName4 = "root";
QString AppConfig::UserPwd4 = "root";

QString AppConfig::ColumnName4 = QString::fromUtf8("序号,时间,用户,通道,类型,内容,备注");
QString AppConfig::ColumnWidth4 = "60,180,80,50,80,100,50";

QString AppConfig::DbType5 = "Sqlite";
QString AppConfig::DbName5 = "dbtool";
QString AppConfig::HostName5 = "127.0.0.1";
int AppConfig::HostPort5 = 3306;
QString AppConfig::UserName5 = "root";
QString AppConfig::UserPwd5 = "root";

QString AppConfig::DbType6 = "Sqlite";
QString AppConfig::DbName6 = "dbtool";
QString AppConfig::HostName6 = "127.0.0.1";
int AppConfig::HostPort6 = 3306;
QString AppConfig::UserName6 = "root";
QString AppConfig::UserPwd6 = "root";

void AppConfig::readConfig()
{
    QSettings set(ConfigFile, QSettings::IniFormat);
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
    set.setIniCodec("utf-8");
#endif

    set.beginGroup("AppConfig");
    IndexDemo = set.value("IndexDemo", IndexDemo).toInt();
    LeftWidth = set.value("LeftWidth", LeftWidth).toInt();
    RightWidth = set.value("RightWidth", RightWidth).toInt();
    set.endGroup();

    set.beginGroup("LocalDbConfig");
    LocalDbType = set.value("LocalDbType", LocalDbType).toString();
    LocalDbName = set.value("LocalDbName", LocalDbName).toString();
    LocalHostName = set.value("LocalHostName", LocalHostName).toString();
    LocalHostPort = set.value("LocalHostPort", LocalHostPort).toInt();
    LocalUserName = set.value("LocalUserName", LocalUserName).toString();
    LocalUserPwd = set.value("LocalUserPwd", LocalUserPwd).toString();
    LocalUserPwd = QUIHelper::getXorEncryptDecrypt(LocalUserPwd, 100);
    set.endGroup();

    set.beginGroup("Db2Config");
    DbType2 = set.value("DbType2", DbType2).toString();
    DbName2 = set.value("DbName2", DbName2).toString();
    HostName2 = set.value("HostName2", HostName2).toString();
    HostPort2 = set.value("HostPort2", HostPort2).toInt();
    UserName2 = set.value("UserName2", UserName2).toString();
    UserPwd2 = set.value("UserPwd2", UserPwd2).toString();
    UserPwd2 = QUIHelper::getXorEncryptDecrypt(UserPwd2, 100);

    SqlColumn2 = set.value("SqlColumn2", SqlColumn2).toString();
    SqlValue2 = set.value("SqlValue2", SqlValue2).toString();
    Count2 = set.value("Count2", Count2).toInt();
    Interval2 = set.value("Interval2", Interval2).toInt();
    set.endGroup();

    set.beginGroup("Db3Config");
    DbType3 = set.value("DbType3", DbType3).toString();
    DbName3 = set.value("DbName3", DbName3).toString();
    HostName3 = set.value("HostName3", HostName3).toString();
    HostPort3 = set.value("HostPort3", HostPort3).toInt();
    UserName3 = set.value("UserName3", UserName3).toString();
    UserPwd3 = set.value("UserPwd3", UserPwd3).toString();
    UserPwd3 = QUIHelper::getXorEncryptDecrypt(UserPwd3, 100);

    Count3 = set.value("Count3", Count3).toInt();
    Interval3 = set.value("Interval3", Interval3).toInt();
    set.endGroup();

    set.beginGroup("Db4Config");
    DbType4 = set.value("DbType4", DbType4).toString();
    DbName4 = set.value("DbName4", DbName4).toString();
    HostName4 = set.value("HostName4", HostName4).toString();
    HostPort4 = set.value("HostPort4", HostPort4).toInt();
    UserName4 = set.value("UserName4", UserName4).toString();
    UserPwd4 = set.value("UserPwd4", UserPwd4).toString();
    UserPwd4 = QUIHelper::getXorEncryptDecrypt(UserPwd4, 100);

    ColumnName4 = set.value("ColumnName4", ColumnName4).toString();
    ColumnWidth4 = set.value("ColumnWidth4", ColumnWidth4).toString();
    set.endGroup();

    set.beginGroup("Db5Config");
    DbType5 = set.value("DbType5", DbType5).toString();
    DbName5 = set.value("DbName5", DbName5).toString();
    HostName5 = set.value("HostName5", HostName5).toString();
    HostPort5 = set.value("HostPort5", HostPort5).toInt();
    UserName5 = set.value("UserName5", UserName5).toString();
    UserPwd5 = set.value("UserPwd5", UserPwd5).toString();
    UserPwd5 = QUIHelper::getXorEncryptDecrypt(UserPwd5, 100);
    set.endGroup();

    set.beginGroup("Db6Config");
    DbType6 = set.value("DbType6", DbType6).toString();
    DbName6 = set.value("DbName6", DbName6).toString();
    HostName6 = set.value("HostName6", HostName6).toString();
    HostPort6 = set.value("HostPort6", HostPort6).toInt();
    UserName6 = set.value("UserName6", UserName6).toString();
    UserPwd6 = set.value("UserPwd6", UserPwd6).toString();
    UserPwd6 = QUIHelper::getXorEncryptDecrypt(UserPwd6, 100);
    set.endGroup();
}

void AppConfig::writeConfig()
{
    QSettings set(ConfigFile, QSettings::IniFormat);
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
    set.setIniCodec("utf-8");
#endif

    set.beginGroup("AppConfig");
    set.setValue("IndexDemo", IndexDemo);
    set.setValue("LeftWidth", LeftWidth);
    set.setValue("RightWidth", RightWidth);
    set.endGroup();

    set.beginGroup("LocalDbConfig");
    set.setValue("LocalDbType", LocalDbType);
    set.setValue("LocalDbName", LocalDbName);
    set.setValue("LocalHostName", LocalHostName);
    set.setValue("LocalHostPort", LocalHostPort);
    set.setValue("LocalUserName", LocalUserName);
    set.setValue("LocalUserPwd", QUIHelper::getXorEncryptDecrypt(LocalUserPwd, 100));
    set.endGroup();

    set.beginGroup("Db2Config");
    set.setValue("DbType2", DbType2);
    set.setValue("DbName2", DbName2);
    set.setValue("HostName2", HostName2);
    set.setValue("HostPort2", HostPort2);
    set.setValue("UserName2", UserName2);
    set.setValue("UserPwd2", QUIHelper::getXorEncryptDecrypt(UserPwd2, 100));

    set.setValue("SqlColumn2", SqlColumn2);
    set.setValue("SqlValue2", SqlValue2);
    set.setValue("Count2", Count2);
    set.setValue("Interval2", Interval2);
    set.endGroup();

    set.beginGroup("Db3Config");
    set.setValue("DbType3", DbType3);
    set.setValue("DbName3", DbName3);
    set.setValue("HostName3", HostName3);
    set.setValue("HostPort3", HostPort3);
    set.setValue("UserName3", UserName3);
    set.setValue("UserPwd3", QUIHelper::getXorEncryptDecrypt(UserPwd3, 100));

    set.setValue("Count3", Count3);
    set.setValue("Interval3", Interval3);
    set.endGroup();

    set.beginGroup("Db4Config");
    set.setValue("DbType4", DbType4);
    set.setValue("DbName4", DbName4);
    set.setValue("HostName4", HostName4);
    set.setValue("HostPort4", HostPort4);
    set.setValue("UserName4", UserName4);
    set.setValue("UserPwd4", QUIHelper::getXorEncryptDecrypt(UserPwd4, 100));

    set.setValue("ColumnName4", ColumnName4);
    set.setValue("ColumnWidth4", ColumnWidth4);
    set.endGroup();

    set.beginGroup("Db5Config");
    set.setValue("DbType5", DbType5);
    set.setValue("DbName5", DbName5);
    set.setValue("HostName5", HostName5);
    set.setValue("HostPort5", HostPort5);
    set.setValue("UserName5", UserName5);
    set.setValue("UserPwd5", QUIHelper::getXorEncryptDecrypt(UserPwd5, 100));
    set.endGroup();

    set.beginGroup("Db6Config");
    set.setValue("DbType6", DbType6);
    set.setValue("DbName6", DbName6);
    set.setValue("HostName6", HostName6);
    set.setValue("HostPort6", HostPort6);
    set.setValue("UserName6", UserName6);
    set.setValue("UserPwd6", QUIHelper::getXorEncryptDecrypt(UserPwd6, 100));
    set.endGroup();
}
